import unittest
from appium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.touch_actions import TouchActions
from venv1.Public.Desired_Config import Desired_Config
from venv1.Page.IcanLoginPage import IcanLoginPage
from venv1.Page.GenerationNo import GenerationNo
import time
from venv1.Public.readexcel import excel_action
import xlrd
from venv1.Public.xlsx_utils import XlsxUtils
from venv1.common.common import common
import traceback

class Test_Iscan(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.desired = Desired_Config.iscanDesired(self)
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", self.desired)
        self.iscanloginpage = IcanLoginPage(self.driver)
        self.generationNo = GenerationNo(self.driver)
        self.location = common(self.driver)
    # @classmethod
    # def tearDownClass(self):
    #     self.driver.quit()

    #iscan登录
    def test01(self):
        size = self.driver.get_window_size()  # 获取手机屏幕大小,分辨率
        print(size)
        time.sleep(8)
        self.iscanloginpage.chooseEnvironment().send_keys("4")
        time.sleep(3)
        print(self.driver.contexts)
        self.iscanloginpage.confirm().click()
        self.driver.switch_to.context("NATIVE_APP")
        time.sleep(10)
        self.iscanloginpage.userType().click()
        time.sleep(1)
        self.iscanloginpage.userName().send_keys("10000796")
        self.iscanloginpage.password().send_keys("080092")
        self.iscanloginpage.loginButton().click()
        time.sleep(3)


    #个人业务生成单号
    def test02(self):
        #读excel数据建单
        filename = r"..\TestData\iscanData.xlsx"
        excelread = xlrd.open_workbook(filename)
        table = excelread.sheet_by_name('Sheet1')
        h =table.nrows
        rownumber = 1
        for i in range(2,h):
            rownumber = rownumber + 1
            excel = excel_action()
            list_data =excel.read_excel_row(filename,0,i)
            print(list_data)
        #新用户信息录入
            try:
                self.driver.switch_to.context("WEBVIEW_cn.com.fwd.iscan")
                time.sleep(10)
                self.generationNo.newCase().click()
                time.sleep(10)
                self.generationNo.newCustomer().click()
                time.sleep(3)
                self.generationNo.name().send_keys(list_data[0])
                self.generationNo.cardId().send_keys(list_data[1])
                self.generationNo.birth().click()
                time.sleep(2)
                self.generationNo.confirm().click()
                if list_data[3] == "男":
                    self.generationNo.sexM().click()
                else:
                    self.generationNo.sexF().click()
                time.sleep(2)
                TouchActions(self.driver).tap(self.generationNo.submit()).perform()
                time.sleep(5)
                self.generationNo.channel().click()
                time.sleep(5)
                self.number = self.generationNo.number().text
                print(self.number)
                xlutil = XlsxUtils()
                time.sleep(2)
                xlutil.set_sheet_value(filename = filename, sheetname = 'Sheet1', row = rownumber + 1, col = 5, value=self.number)
                #生成单号后，点击下一步
                self.generationNo.next().click()
                time.sleep(5)
                self.driver.switch_to.context("WEBVIEW_cn.com.fwd.iscan")
                time.sleep(2)
                if list_data[5] == '客户基本信息资料':
                    self.generationNo.customerInformation01().click()
                elif list_data[5] == "新业务资料":
                    self.generationNo.customerInformation02().click()
                elif list_data[5] == "客户健康资料":
                    self.generationNo.customerInformation03().click()
                elif list_data[5] == "客户财务资料":
                    self.generationNo.customerInformation04().click()
                time.sleep(2)
                if list_data[6] == "身份证":
                    self.generationNo.informationType01().click()
                elif list_data[6] == "其他证件影像":
                    self.generationNo.informationType02().click()
                elif list_data[6] == "个人税收居民身份告知":
                    self.generationNo.informationType03().click()
                elif list_data[6] == "W8/W9":
                    self.generationNo.informationType04().click()
                elif list_data[6] == "关系证明":
                    self.generationNo.informationType05().click()
                self.generationNo.Box().send_keys(list_data[7])
                time.sleep(2)
                self.generationNo.upload().click()
                time.sleep(1)
                self.driver.switch_to.context("NATIVE_APP")
                time.sleep(3)
                # self.location.location(712, 708)
                # time.sleep(1)
                # self.location.location(985, 497)
                # time.sleep(5)
                # self.location.location(1814, 1136)
                self.location.location(753, 657)
                time.sleep(2)
                self.location.location(898, 477)
                time.sleep(2)
                self.location.location(1815, 1044)
                time.sleep(8)
                self.driver.switch_to.context("WEBVIEW_cn.com.fwd.iscan")
                time.sleep(1)
                self.generationNo.submitInfomation().click()
                time.sleep(1)
                self.generationNo.submit().click()
                time.sleep(2)
                self.generationNo.iconMenu().click()
                time.sleep(1)
                self.generationNo.homePage().click()
                time.sleep(2)
            except Exception:
                print("有异常")
                traceback.print_exc()
                self.driver.quit()
                time.sleep(1)
                Test_Iscan.setUpClass()
                time.sleep(1)
                Test_Iscan.test01(self)

